﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using ArcEngine.Resources;
using System.Globalization;

namespace ArcEngine.Editor.Wizards
{
	public partial class NewTTFFontWizard : Form
	{

		/// <summary>
		/// 
		/// </summary>
		public NewTTFFontWizard()
		{
			InitializeComponent();


			SelectFontBox.Items.Clear();
			foreach (string name in ResourceManager.GetBinaries())
			{
				if (name.EndsWith(".ttf", true, CultureInfo.CurrentCulture))
					SelectFontBox.Items.Add(name);
			}

		}


		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void NewTTFFontWizard_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (DialogResult != DialogResult.OK)
				return;

			// Font already exists ?
			if (ResourceManager.GetTTFFont(FontNameBox.Text) != null || FontNameBox.Text == "")
			{
				MessageBox.Show("Font name already in use or invalid. Use another name !");
				e.Cancel = true;
				return;
			}

			if (fileName == null || fileName == "")
			{
				MessageBox.Show("Select a font first !");
				e.Cancel = true;
				return;
			}



			// Adds the font file as a binary
			if (fileNameFullPath != null)
			{
				Binary bin = ResourceManager.CreateBinary(fileName);
				if (bin != null)
					bin.LoadFile(fileNameFullPath);

			}


			TTFFont font = ResourceManager.CreateTTFFont(FontNameBox.Text);
			if (font != null)
			{
				font.Generate(fileName, font.Size, font.Style);
			}

		}


		#region Events

		/// <summary>
		/// FontSelector OnClick
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void LoadButton_Click(object sender, EventArgs e)
		{
			if (OpenFileBox.ShowDialog() != DialogResult.OK)
				return;

			fileNameFullPath = OpenFileBox.FileName;
			fileName = OpenFileBox.SafeFileName;
			FileNameBox.Text = "Selected font : " + fileName;
		}

		/// <summary>
		/// Selects a font from ResourceManager, so no loading to do
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void SelectFontBox_SelectedIndexChanged(object sender, EventArgs e)
		{
			fileName = (string)SelectFontBox.SelectedItem;
			fileNameFullPath = null;
			FileNameBox.Text = "Selected font : " + fileName;
		}


		#endregion


		#region Variables


		/// <summary>
		/// Name of the TTF file
		/// </summary>
		string fileName;

		/// <summary>
		/// Name of the full path of the TTF file
		/// </summary>
		string fileNameFullPath;
							
									
		#endregion



	}
}
